package com.tanhua.app.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.BlackListApi;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.SettingsApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.SettingsVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;

    public ResponseEntity findSettingsVo() {
        SettingsVo vo = new SettingsVo();
        // 1、获取当前登录人user对象
        User user = UserHolder.getUser();
        // 2、查询陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        if (question != null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        // 3、查询通知设置
        Settings settings = settingsApi.findByUserId(user.getId());
        if (settings != null) {
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        // 4、设置手机号
        vo.setPhone(user.getPhone());
        // 5、返回vo对象
        return ResponseEntity.ok(vo);
    }

    public ResponseEntity saveQuestion(String content) {
        // 1、获取用户userId
        Long userId = UserHolder.getUserId();
        // 2、根据userId查询question
        Question question = questionApi.findByUserId(userId);
        // 3、判断
        if (question == null) { // 新增
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionApi.save(question);
        } else { // 修改
            question.setStrangerQuestion(content);
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity saveSettings(Settings settings) {
        // 1、获取userId
        Long userId = UserHolder.getUserId();

        // 2、根据userId查询settings
        Settings findSettings = settingsApi.findByUserId(userId);

        // 3、判断
        if (findSettings == null) { // 新增
            settings.setUserId(userId);
            settingsApi.save(settings);
        } else { // 修改
            findSettings.setLikeNotification(settings.getLikeNotification());
            findSettings.setPinglunNotification(settings.getPinglunNotification());
            findSettings.setGonggaoNotification(settings.getGonggaoNotification());
            settingsApi.update(findSettings);
        }

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findBlackListPage(Integer pageNum, Integer pageSize) {

        // 1、获取userId
        Long userId = UserHolder.getUserId();

        // 2、调用api实现分页查询
        IPage<UserInfo> iPage = blackListApi.findBlackList(pageNum, pageSize, userId);

        // 3、封装PageBeanVo
        PageBeanVo vo = new PageBeanVo(pageNum, pageSize, iPage.getTotal(), iPage.getRecords());

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity deleteBlack(Long blackUserId) {
        // 1、获取userId
        Long userId = UserHolder.getUserId();
        // 2、调用api移出黑名单
        blackListApi.deleteBlack(userId, blackUserId);

        return ResponseEntity.ok(null);
    }
}
